package com.pica.system.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import com.pica.common.constant.UserConstants;
import com.pica.common.core.domain.entity.SysUser;
import com.pica.common.utils.StringUtils;
import com.pica.system.domain.Evaluate;
import com.pica.system.domain.EvaluateResume;
import com.pica.system.service.IEvaluateService;
import com.pica.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pica.common.annotation.Log;
import com.pica.common.enums.BusinessType;
import com.pica.system.domain.Resume;
import com.pica.system.service.IResumeService;
import com.pica.common.core.controller.BaseController;
import com.pica.common.core.domain.AjaxResult;
import com.pica.common.utils.poi.ExcelUtil;
import com.pica.common.core.page.TableDataInfo;

/**
 * 简历Controller
 * 
 * @author 徐鑫杰
 * @date 2022-12-14
 */
@Controller
@RequestMapping("/system/resume")
public class ResumeController extends BaseController
{
    private String prefix = "system/resume";

    @Autowired
    private IResumeService resumeService;
    @Autowired
    private IEvaluateService evaluateService;
    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("system:resume:view")
    @GetMapping()
    public String resume()
    {
        return prefix + "/resume";
    }

    /**
     * 面试一览
     */
    @RequiresPermissions("system:resume:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Resume resume)
    {
        startPage();
        List<Resume> list = resumeService.selectResumeList(resume);

        return getDataTable(list);
    }

    /**
     * 导出简历列表
     */
    @RequiresPermissions("system:resume:export")
    @Log(title = "简历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Resume resume)
    {
        List<Resume> list = resumeService.selectResumeList(resume);
        ExcelUtil<Resume> util = new ExcelUtil<Resume>(Resume.class);
        return util.exportExcel(list, "简历数据");
    }

    /**
     * 简历文件下载，
     */
    @RequiresPermissions("system:resume:download")
    @GetMapping("/download/{resId}")
    public String resourceDownload(@PathVariable("resId") Long resId) throws UnsupportedEncodingException
    {
        Resume resume = resumeService.selectResumeByResId(resId);
        String url=resume.getResFile();
        url=StringUtils.substringAfter(url, "upload/");
        return "redirect:/common/download?fileName="+ URLEncoder.encode(url,"UTF-8")+"&delete=" + false;
    }

    /**
     * 新增简历
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("user", userService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增 简历上传
     */
    @RequiresPermissions("system:resume:add")
    @Log(title = "简历", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Resume resume)
    {
        if (StringUtils.isNotEmpty(resume.getResTel())
                && UserConstants.USER_PHONE_NOT_UNIQUE.equals(resumeService.checkPhoneUnique(resume)))
        {
            return error("新增简历'" +resume.getResName()+ "'失败，手机号码已存在");
        }
        //设置的修改人和创建人默认为登录系统的用户
        resume.setCu(getUserId());
        resume.setUu(getUserId());
        return toAjax(resumeService.insertResume(resume));
    }

    /**
     * 修改简历
     */
    @RequiresPermissions("system:resume:edit")
    @GetMapping("/edit/{resId}")
    public String edit(@PathVariable("resId") Long resId, ModelMap mmap)
    {
        Resume resume = resumeService.selectResumeByResId(resId);
        mmap.put("resume", resume);
        return prefix + "/edit";
    }

    /**
     * 简历review
     */
    @RequiresPermissions("system:resume:review")
    @GetMapping("/review/{resId}")
    public String review(@PathVariable("resId") Long resId, ModelMap mmap)
    {
        Resume resume = resumeService.selectResumeByResId(resId);
        mmap.put("resume", resume);
        return prefix + "/review";
    }


    /**
     * 更新面试官
     */
    @RequiresPermissions("system:resume:interview")
    @GetMapping("/interview/{resId}")
    public String interview(@PathVariable("resId") Long resId, ModelMap mmap)
    {
        if(null == evaluateService.selectEvaluateByEvaId(resId) ||"".equals(evaluateService.selectEvaluateByEvaId(resId))){
            Evaluate evaluate=new Evaluate();
            //设置面试评价表的id与简历的id一致
            evaluate.setEvaId(resId);
            evaluate.setResId(resId);
            evaluateService.insertEvaluate(evaluate);
        }

        Evaluate evaluate = evaluateService.selectEvaluateByEvaId(resId);
        mmap.put("evaluate", evaluate);
        Resume resume = resumeService.selectResumeByResId(resId);
        mmap.put("resume", resume);
        return prefix + "/interview";
    }


    /**
     * 面试评价表
     */
    @RequiresPermissions("system:resume:evaluate")
    @GetMapping("/evaluate/{resId}")
    public String evaluate(@PathVariable("resId") Long resId, ModelMap mmap)
    {
        if(null == evaluateService.selectEvaluateByEvaId(resId) ||"".equals(evaluateService.selectEvaluateByEvaId(resId))){
            Evaluate evaluate=new Evaluate();
            evaluate.setEvaId(resId);
            evaluate.setResId(resId);
            evaluateService.insertEvaluate(evaluate);
        }

        Evaluate evaluate = evaluateService.selectEvaluateByEvaId(resId);
        mmap.put("evaluate", evaluate);
        Resume resume = resumeService.selectResumeByResId(resId);
        mmap.put("resume", resume);
        return prefix + "/evaluate";
    }


    /**
     * 简历review的提交按钮
     */
    @RequiresPermissions("system:resume:review")
    @Log(title = "简历review", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Resume resume)
    {
        resume.setUu(getUserId());
        return toAjax(resumeService.updateResume(resume));
    }


    /**
     * 评价表提交的接口
     */
    @Log(title = "评价表提交的接口", businessType = BusinessType.UPDATE)
    @PostMapping("/evaluate")
    @ResponseBody
    public AjaxResult evaluateSave(EvaluateResume evaluateresume)
    {
        return toAjax(resumeService.updateEvaluateResume(evaluateresume));
    }

    /**
     * 删除简历
     */
    @RequiresPermissions("system:resume:remove")
    @Log(title = "简历", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(resumeService.deleteResumeByResIds(ids));
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(Resume resume)
    {
        return resumeService.checkPhoneUnique(resume);
    }

}
